package cl.mt.mvc.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import cl.mt.dto.DTOEmpresa;
import cl.mt.mvc.action.oferente.PublicarOfertaCargaAction;
import cl.mt.mvc.helper.EmpresaHelper;



public class RegistroEmpresaAction extends Action {

	private Logger log = LogManager.getLogger(PublicarOfertaCargaAction.class.getName());
	
	  public ActionForward execute (ActionMapping mapping, ActionForm form,

          HttpServletRequest request, HttpServletResponse response) {
		  
		  String forward= "ERROR";
		  log.debug("RegistroEmpresaAction..........");
		  
		  String accion= "inicio";
		  log.debug("campo accion:["+accion+"]");
		  
		  if(request.getParameter("accion")!=null){
		  	accion=request.getParameter("accion");
		  }
		 
		  if("inicio".equals(accion)){
			  log.debug("cargar  el formulario.");				
			  forward="SUCCESS";
		
		  }
		  else if ("addEmpresa".equals(accion)){
				log.debug("Ciudad: "+request.getParameter("comunaInput"));
			  DTOEmpresa empresa = new DTOEmpresa();
			  empresa.setRazonsocial(request.getParameter("razonSocialInput"));
			  empresa.setRut(request.getParameter("rutInput"));
			  empresa.setNombreFantasia(request.getParameter("nomFantasiaInput"));
			  empresa.setGiro(request.getParameter("giroInput"));				
			  empresa.setDireccionCalle(request.getParameter("calleInput"));
			  empresa.setDireccionNumero(request.getParameter("numeroInput"));				
			  empresa.setTelefono(request.getParameter("telefonoInput"));
			  empresa.setMail(request.getParameter("emailInput"));
			  empresa.setUrlWeb(request.getParameter("sitioWebInput"));
			  empresa.setIdCiudad(new Integer(request.getParameter("comunaInput")));
			  
			  
			  
			  
			  //check!!
			  EmpresaHelper nvaEmpresa = new EmpresaHelper();
			  int res = nvaEmpresa.nuevaEmpresa(empresa);
			 if(res== 1){
				 ActionRedirect redirect =new ActionRedirect(mapping.findForward("registroUsuario"));
				 return redirect;
			 }
			 else forward="SUCCESS";
		
		  
		  }else if ("addUsuario".equals(accion)){
			  log.debug("guardar contenido del formulario en action");
			  

			  
			  forward="SUCCESS";
		  
		  
		  }else{
			  
			  log.debug("No hay accion a ejecutar.");
			  forward="ERROR";
		  }
		  
		  return mapping.findForward(forward);
	  }

	  

	 
	  
}
